package controller;

import com.google.gson.Gson;
import pojo.Businesses;
import service.IBusinessManagementService;
import service.imppl.BusinessManagementService;
import util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@WebServlet("/businessControl/*")
public class BusinessController extends HttpServlet {
    IBusinessManagementService ibms=new BusinessManagementService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        if (mName.length() == 0) {
            mName = "showdt";
        }
        Class cla = this.getClass();
        Method method = null;
        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public void businessList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String bName=request.getParameter("bName");
        Businesses businesses=new Businesses();
        businesses.setbName(bName);
        PageHelper ph=new PageHelper();
        int i=ibms.queryCountBusiness(businesses);
        ph.setDateCount(i);
        int indexInt=Integer.parseInt(request.getParameter("indexPage"));
        ph.setPageIndex(indexInt);
        ArrayList<Businesses> businesses1=ibms.queryBusiness(businesses,ph);
        ph.setListIndex(businesses1);
        response.getWriter().println(new Gson().toJson(ph));
    }
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String bId=request.getParameter("bId");
        boolean b=ibms.removeBusinesses(Integer.parseInt(bId));
        response.getWriter().println(new Gson().toJson(b));
    }
    public void allBusinesses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        ArrayList allBusinesses=ibms.queryAllBusinesses();
        response.getWriter().println(new Gson().toJson(allBusinesses));
    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        int bId=Integer.parseInt(request.getParameter("bId"));
        String bName=request.getParameter("bName");
        String description=request.getParameter("description");
        String status=request.getParameter("status");
        String moneyStr=request.getParameter("money");
        Businesses businesses=new Businesses();
        businesses.setbId(bId);
        businesses.setbName(bName);
        businesses.setStatus(status);
        try {
            double money = Double.parseDouble(moneyStr);
            businesses.setMoney(money);
        } catch (NumberFormatException e) {
            System.out.println("字符串格式错误，无法转换为double");
        }
        businesses.setDescription(description);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String startDateStr=request.getParameter("startDateStr");
            if (startDateStr != null && !startDateStr.trim().isEmpty()) {
                Date startDate = formatter.parse(startDateStr);
                businesses.setStartDate(startDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            String endDateStr=request.getParameter("endDateStr");
            if (endDateStr != null && !endDateStr.trim().isEmpty()) {
                Date endDate = formatter.parse(endDateStr);
                businesses.setEndDate(endDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        boolean b=ibms.changeBusiness(businesses);
        response.getWriter().println(new Gson().toJson(b));
    }
    // 添加getBusinessDetail方法
    public void getBusinessDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        try {
            String bId = request.getParameter("bId");
            if (bId != null && !bId.trim().isEmpty()) {
                // 调用Service层获取业务详情
                ArrayList<Businesses> businesses = ibms.queryBusinessById(Integer.parseInt(bId));
                if (businesses != null && !businesses.isEmpty()) {
                    response.getWriter().println(new Gson().toJson(businesses.get(0)));
                    return;
                }
            }
            response.getWriter().println(new Gson().toJson(null));
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(new Gson().toJson(null));
        }
    }
    public void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        Businesses businesses=new Businesses();
        businesses.setbName(request.getParameter("name"));
        businesses.setDescription(request.getParameter("description"));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String startDateStr=request.getParameter("startDate");
            if (startDateStr != null && !startDateStr.trim().isEmpty()) {
                Date startDate = formatter.parse(startDateStr);
                businesses.setStartDate(startDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            String endDateStr=request.getParameter("endDate");
            if (endDateStr != null && !endDateStr.trim().isEmpty()) {
                Date endDate = formatter.parse(endDateStr);
                businesses.setEndDate(endDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        businesses.setStatus(request.getParameter("status"));
        try {
            double money = Double.parseDouble(request.getParameter("money"));
            businesses.setMoney(money);
        } catch (NumberFormatException e) {
            System.out.println("字符串格式错误，无法转换为double");
        }
        boolean b=ibms.addBusiness(businesses);
        response.getWriter().println(new Gson().toJson(b));
    }
}